const webpack = require('webpack');
const fs = require('fs');
const Dotenv = require('dotenv');
const path = require('path');
const DashboardPlugin = require('webpack-dashboard/plugin'),
    UglifyJsPlugin = require('uglifyjs-webpack-plugin'),
    APP_PATH = path.resolve(__dirname, './src/index.js'),
    BUILD_PATH = path.resolve(__dirname, './dist');

function resolve(dir) {
    return path.join(__dirname, dir);
}
const workDir = process.cwd();
const s = Dotenv.parse(fs.readFileSync(path.join(workDir, '.env.production')));
module.exports = {
    entry: './src/index.js',
    output: {
        path: BUILD_PATH,
        filename: '[name].js', // 输出js
        libraryTarget: 'umd'
    },
    externals: {
        lodash: {
            commonjs: 'lodash',
            commonjs2: 'lodash',
            amd: 'lodash',
            root: '_'
        },
        axios: {
            commonjs: 'axios',
            commonjs2: 'axios',
            amd: 'axios',
            root: 'axios'
        }
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                use: {
                    loader: 'babel-loader', 
                    options: { 
                        presets: ['@babel/preset-env'] 
                    } 
                }
            },
            {
                test: /\.glsl$/,
                use: {
                    loader: 'webpack-glsl-loader', 
                }
            },
            {
                test: /\.worker\.js$/,
                use: [{
                    loader: 'worker-loader',
                    options: {
                        inline: 'fallback'
                    }
                }]
            },
        ]
    },
    optimization: {
        minimize: true,
        splitChunks: {
            chunks: 'async',
            minSize: 10000,
            maxSize: 50000,
            minChunks: 1,
            maxAsyncRequests: 5,
            maxInitialRequests: 3,
            automaticNameDelimiter: '~',
            automaticNameMaxLength: 30,
            name: true,
            cacheGroups: {
                vendors: {
                    test: /[\\/]node_modules[\\/]/,
                    priority: -10
                },
                default: {
                    minChunks: 2,
                    priority: -20,
                    reuseExistingChunk: true
                }
            }
        }
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env': JSON.stringify(s)
        })
    ],
    mode: 'production'
};
